package br.com.mooble.activitys.test;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.HeaderProperty;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.Marshal;
import org.ksoap2.serialization.MarshalFloat;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.util.Log;

public class WSClientHelper {
	private static final String SOAP_ACTION = "http://ws.ufsc.br/services/";
	private static final String METHOD_NAME = "getGradeHorarioAluno";
	private static final String NAMESPACE = "http://services.cagr.ufsc.br/";
	
	private static final String WSDL = "http://ws.ufsc.br/services/CAGRService";

	private String response;

	public static void main(String[] args) {
		WSClientHelper client = new WSClientHelper();
		System.out.println(client.getResponse());
	}

	public WSClientHelper() {

		SoapObject request = new SoapObject(null, METHOD_NAME);
		request.addAttribute("xmlns", NAMESPACE);
		request.addProperty("matricula", 9238050l);
		
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.setAddAdornments(false);
		envelope.setOutputSoapObject(request);
		envelope.implicitTypes = true;
					
		HttpTransportSE httpTransport = new HttpTransportSE(WSDL);
		httpTransport.debug = true; 
		
		try {

			List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();
			headerList.add(new HeaderProperty("Authorization", "Basic " + org.kobjects.base64.Base64.encode("tccmarlon:tcda5s53".getBytes())));
			
			httpTransport.call(SOAP_ACTION + METHOD_NAME, envelope, headerList);
			
			Log.d("SOAP", "HTTP REQUEST:\n" + httpTransport.requestDump);
			Log.d("SOAP", "HTTP RESPONSE:\n" + httpTransport.responseDump);
			
			SoapObject response = (SoapObject) envelope.getResponse();
			setResponse(response.toString());

		} catch (Exception e) {
			System.out.println("Encontrei um erro no SOAP");
			System.out.println(httpTransport.requestDump);
			System.out.println(httpTransport.responseDump);
			e.printStackTrace();
		}
	}

	public String getResponse() {
		return response;
	}

	public void setResponse(String response) {
		this.response = response;
	}
	
	
	
	/*private static final String METHOD_NAME = "TopGoalScorers";
	 private static final String SOAP_ACTION = "http://footballpool.dataaccess.eu/data/TopGoalScorers&quot;;
	 private static final String NAMESPACE = "http://footballpool.dataaccess.eu";
	 private static final String URL = "http://footballpool.dataaccess.eu/data/info.wso?WSDL&quot;;
	//you can get these values from the wsdl file^
	 */
	
	 public SoapObject soap(String METHOD_NAME, String SOAP_ACTION, String NAMESPACE, String URL) throws Exception {
		 
	    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); //set up request
	    request.addProperty("iTopN", "5"); //variable name, value. I got the variable name, from the wsdl file!
	    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); //put all required data into a soap envelope
	    envelope.setOutputSoapObject(request);  //prepare request
	    HttpTransportSE httpTransport = new HttpTransportSE(URL);  
	    httpTransport.debug = true;  //this is optional, use it if you don't want to use a packet sniffer to check what the sent message was (httpTransport.requestDump)
	    httpTransport.call(SOAP_ACTION, envelope); //send request
	    SoapObject result=(SoapObject)envelope.getResponse(); //get response
	    return result;
	    
	 }
}
